package entrata;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  Servlet utilizzata  per creare/cambiare passworde e username
 * 
 * @author sestari
 */
public class registrarePaziente extends HttpServlet {

    /**
     * Generazione della pagina HTML per inserire nome, cognome , data nascita
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Calendar calendar = new GregorianCalendar();
            DateFormat dataf = new SimpleDateFormat("dd'/'MM'/'yyyy");
            String dataformattata = dataf.format(calendar.getTime());



            out.println("<html>");
            out.println("<head>");
            out.println("<title>Registrazione Paziente</title>");
            out.println("</head>");
            out.println("<body>");
            String msg = request.getParameter("msgerro");
            if (msg != null) {
                out.println("<br>");
                out.println("<b>");
                out.println(msg);
                out.println("</b>");
                out.println("<br>");
                out.println("<br>");
            }

            out.println("Incerice le sue informazione per aggiungere o cambiare Utente o Password: <br>");
            out.println("        <form action=\"registrarePaziente\"   method=\"POST\" > ");
            out.println("            <table border=\"0\"> ");
            out.println("                <tr> ");
            out.println("                    <td bgcolor=\"#DEDFF7\"> ");
            out.println("                        Nome: ");
            out.println("                    </td> ");
            out.println("                    <td bgcolor=\"white\"> ");
            out.println("                        <input type=\"text\" name=\"nome\"/> ");
            out.println("                    </td> ");
            out.println("                </tr> ");
            out.println("                <tr> ");
            out.println("                    <td bgcolor=\"#DEDFF7\"> ");
            out.println("                        Cognome: ");
            out.println("                    </td> ");
            out.println("                    <td bgcolor=\"white\"> ");
            out.println("                        <input type=\"text\" name=\"cognome\"/> ");
            out.println("                    </td> ");
            out.println("                </tr> ");
            out.println("                <tr> ");
            out.println("                    <td bgcolor=\"#DEDFF7\"> ");
            out.println("                        Data nascita: ");
            out.println("                    </td> ");
            out.println("                    <td bgcolor=\"white\"> ");
            out.println("                        <input type=\"text\" name=\"data\" value=\"" + dataformattata + "\" /> ");
            out.println("                    </td> ");
            out.println("                </tr> ");
            out.println("                <tr>       ");
            out.println("                    <td> ");
            out.println("                        <input type=\"submit\" name=\"inviare\" value=\"Verificare\"/> ");
            out.println("                        </form> ");
            out.println("                    </td> ");
            out.println("                    <td>  ");
            out.println("                <form action=\"index.html\" method=\"post\">  ");
            out.println("                        <input type=\"submit\" name=\"tornare\" value=\"Tornare\"/> ");
            out.println("                </form>  ");
            out.println("                    </td>                    ");
            out.println("                    </tr> ");
            out.println("                </table> ");
            out.println("                </form> ");
            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }

    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String data = request.getParameter("data");
            String nome = request.getParameter("nome");
            String cognome = request.getParameter("cognome");
            SimpleDateFormat temp = new SimpleDateFormat("dd/MM/yyyy");
            Date d = new java.sql.Date(temp.parse(data).getTime());
            String dataformat = d.toString();


            ServletContext sc = getServletContext();
            Connection con = (Connection) sc.getAttribute("con");


            String id = request.getParameter("id");
            String comand = "SELECT id from Persone where nome=\"" + nome + "\" and cognome=\"" + cognome + "\" and data_nascita=\"" + dataformat + "\"";


            Statement st = (Statement) con.createStatement();

            ResultSet rs = (ResultSet) st.executeQuery(comand);


            if (rs.next()) {
                html(request, response, rs.getString(1));

            } else {
                response.sendRedirect("index.html?msg=Paziente non essistente!");
            }

        } catch (SQLException ex) {
            Logger.getLogger(registrarePaziente.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(registrarePaziente.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    /**
     * Generazione della pagina HTML per inserire username e password
     * 
     * @param request
     * @param response
     * @param id
     * @throws ServletException
     * @throws IOException 
     */
    protected void html(HttpServletRequest request, HttpServletResponse response, String id)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<!DOCTYPE html>   ");
            out.println("<html>  ");
            out.println("    <head>  ");
            out.println("        <title>Registrazione</title> ");
            out.println("    </head>  ");
            out.println("    <body>  ");
            out.println("        <form action=\"registrazione\"   method=\"POST\" > ");
            out.println("             <input type=\"hidden\" name=\"id\"  value=\"" + id + "\" /> ");
            out.println("            <table border=\"0\"> ");

            out.println("                <tr> ");
            out.println("                    <td bgcolor=\"#DEDFF7\"> ");
            out.println("                        User Name: ");
            out.println("                    </td> ");
            out.println("                    <td bgcolor=\"white\"> ");
            out.println("                        <input type=\"text\" name=\"username\"/> ");
            out.println("                    </td> ");
            out.println("                </tr> ");


            out.println("                <tr> ");
            out.println("                    <td bgcolor=\"#DEDFF7\"> ");
            out.println("                        Password: ");
            out.println("                    </td> ");
            out.println("                    <td bgcolor=\"white\"> ");
            out.println("                        <input type=\"password\" name=\"password\"/> ");
            out.println("                    </td> ");
            out.println("                </tr> ");


            out.println("                <tr>       ");
            out.println("                    <td> ");
            out.println("                        <input type=\"submit\" name=\"inviare\" value=\"creare\"/> ");
            out.println(" ");
            out.println("                        </form> ");
            out.println("                    </td> ");
            out.println("                <form action=\"index.html\" method=\"post\">  ");
            out.println("                    <td>  ");
            out.println("                        <input type=\"submit\" name=\"tornare\" value=\"Tornare\"/> ");
            out.println("                    </td>                    ");
            out.println("                </form>  ");
            out.println("                </tr>  ");
            out.println(" ");
            out.println("            </table> ");
            out.println("    </body>  ");
            out.println("</html>  ");

        } finally {
            out.close();
        }
    }
}
